<template>
	<view class="container" v-if="type == 'nickname'">
		<view class="cu-form-group margin-top">
			<input focus placeholder="用户昵称2-6个字符，由英文、数字、中文组成" v-model="nickname" maxlength="6"/>
		</view>
		<view class="padding-sm text-gray">用户昵称2-6个字符，由英文、数字、中文组成</view>
		<view class="margin-lr-lg padding-0 submit_btn">
			<button class="cu-btn" type="button" @click="updateData">完成修改</button>
		</view>
	</view>

</template>


<script>
	export default {
		data() {
			return {
				type:'nickname',
				nickname:'',
				userinfo:[]
			};
		},
		onNavigationBarButtonTap(val) {
			this.updateData();
		},
		onLoad: function (option) {
			uni.setNavigationBarTitle({
				title: '修改昵称'
			});
			this.helper.checkLogin();
			this.getData();
		},
		methods: {
			getData:function(){
				//获取基础参数
				// uni.showLoading({
				// 	title:"数据加载中",
				// 	mask:true
				// })
				var info = uni.getStorageSync("userinfo");
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(['username='+info.username,'token='+info.token,'timestamp='+timestamp]);
				this.helper.req({
					api:"getUserInfo",
					timestamp:timestamp,
					data:{username:info.username,token:info.token},
					header:{sign:sign},
					method:'GET',
				}).then(res=>{
					this.userinfo = res.data.data.userinfo;
					this.nickname = res.data.data.userinfo.nickname;
					
					setTimeout(() => {
						uni.hideLoading();
					}, 300);
				})
			},
			updateData:function(){
				if(this.nickname == ''){
					uni.showToast({
						title:"请输入昵称",
						icon:"none"
					})
					return;
				}
				//获取基础参数
				uni.showLoading({
					title:"数据更新中",
					mask:true
				})
				var info = uni.getStorageSync("userinfo");
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(['nickname='+this.nickname,'timestamp='+timestamp,'token='+info.token]);
				this.helper.req({
					api:"updateNickname",
					timestamp:timestamp,
					data:{nickname:this.nickname,token:info.token},
					header:{sign:sign},
					method:'POST',
				}).then(res=>{
					uni.showToast({
						title: "昵称更新成功",
						icon: "success"
					})
					setTimeout(() => {
						uni.hideLoading();
						uni.redirectTo({
							url: '/pages/user/userinfo',
						});	
					}, 300);
				})
			},
		},
	};
</script>

<style lang="scss">
	page{
		background-color: #ddeafd;
	}
	
</style>